﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Domain;
using Domain.Common;
using Domain.Officers;

namespace Service
{
    /// <summary>
    /// 人才服务
    /// </summary>
   public class TalentsService
    {
        private static Random rd = new Random(System.DateTime.Now.Millisecond);
        public static List<Officer> CanVisitOfficers=new List<Officer>();

        public static void StartToTalentsWork(Force force, int days)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateOfficerLogin), null);
            ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateCanVisitOfficers), null);
            foreach (var building in force.Buildings)
            {
                AIdispatchVisiteJob(building);
                //执行登庸
                AIStartVisitOfficerWork(building);

            }

        }
       /// <summary>
       /// AI调整官位
       /// </summary>
        private static void AIAdjustOfficerPosition()
       {
       }
        /// <summary>
        /// AI执行登庸工作
        /// </summary>
        /// <param name="building"></param>
        private static void AIStartVisitOfficerWork(Building building)
        {
            foreach (var canVisitOfficer in building.OwnedOfficers.Where(x => x.WorkType == WorkType.VisiteOfficer))
            {
                canVisitOfficer.UpdateVisiteOfficer();
            }
        }
        /// <summary>
       /// 每个回合检查是否有武将登场
       /// </summary>
        public static void UpdateOfficerLogin(object o)
        {
            foreach (KeyValuePair<int, Officer> pair in Officer.UnOfficeOfficers)
            {
                if (pair.Value.YearAvailable >= HistoryDate.CurrentDate.Year)
                {
                    pair.Value.State = OfficerState.OutOffice;
                    Building building = Building.Buildings[(short)pair.Value.LoginBuildingId];
                    building.InBuildingOfficers.Add(pair.Value);
                    pair.Value.InBuilding = building;
                    Officer.UnOfficeOfficers.Remove(pair.Key);
                }
            }
        }
       /// <summary>
       /// 更新全部可以登庸拜访的武将
       /// </summary>
       /// <returns></returns>
        public static void UpdateCanVisitOfficers(object o)
       {
          
            CanVisitOfficers.Clear();
            foreach (KeyValuePair<int, Officer> pair in Officer.Officers)
            {
                if (pair.Value.InBuilding!=null&&(pair.Value.State==OfficerState.OnOffice||pair.Value.State==OfficerState.OutOffice))
                {
                    CanVisitOfficers.Add(pair.Value);
                }
            }
        }
        /// <summary>
        /// 分配登庸工作任务
        /// </summary>
        /// <para></para>
        /// <returns></returns>
        public static bool DispatchVisiteJob(int officerId,int targetOfficerId)
        {
            Officer officer=Officer.Officers[officerId];
            Officer targetOfficer = Officer.Officers[targetOfficerId];
            int days = officer.InBuilding.ComputeDaysFromTargetBuilding(targetOfficer.InBuilding);
            VisiteOfficer visiteOfficer=new VisiteOfficer()
            {
                StarTime = HistoryDate.CurrentDate,
                ArrivalTime = HistoryDate.CurrentDate.AddDays(days),
                BackTime = HistoryDate.CurrentDate.AddDays(days*2),
                IsSuccess = false,
                TargetBuilding = officer.InBuilding,
                TargetOfficer = targetOfficer
            };
            officer.WorkType = WorkType.VisiteOfficer;
            officer.VisiteOfficer = visiteOfficer;
            return true;
        }
       /// <summary>
        ///  AI分配登庸工作
       /// </summary>
       /// <param name="building"></param>
        public static void AIdispatchVisiteJob(Building building)
       {
           if(building.OwnedOfficers.Any(x=>x.WorkType==WorkType.VisiteOfficer))
               return;
           List<Officer> list= CanVisitOfficers.Where(x => x.State == OfficerState.OutOffice).ToList();
           int val = rd.Next(0, list.Count);
           Officer targetOfficer = list[val];
           List<Officer> list2 = building.OwnedOfficers.Where(x => x.InBuilding.Id == building.Id).ToList();
           int val2 = rd.Next(0, list2.Count);
           Officer visiteOfficer = list2[val2];
           DispatchVisiteJob(visiteOfficer.Id, targetOfficer.Id);
       }

        public static void ApointOfficialPosition(short positionId, int officerId)
        {
            Officer officer = Officer.Officers[officerId];
            OfficialPosition position = OfficialPosition.OPositions[positionId];
            if(position.CurrentOfficers.Any(x=>x.Id==officer.Id))
                return;
            position.CurrentOfficers.Add(officer);
            officer.OfficialPosition = position;
        }
    }
}
